package com.example.huawei.system.task;

import com.example.huawei.system.entity.ReserveMaintain;
import com.example.huawei.system.service.ReserveMaintainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Component
public class Task {

    private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    @Autowired
    private ReserveMaintainService reserveMaintainService;

    @Scheduled(cron = "0 0 12 * * ?")
    public void typeUpdateTask() {
        List<ReserveMaintain> list = reserveMaintainService.lambdaQuery()
                .le(ReserveMaintain::getReserveTime, new Date())
                .eq(ReserveMaintain::getType, 1)
                .list();

        String nowDate = dateFormat.format(new Date());

        list.forEach(item->{
            if (dateFormat.format(item.getReserveTime()).equals(nowDate))
                item.setType(2);
        });

        reserveMaintainService.updateBatchById(list);
    }
}
